package com.hs.app.breakcall.view;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hs.app.breakcall.R;
import com.hs.app.breakcall.adapter.IntroAdapter;
import com.hs.app.breakcall.common.Const;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

public class Intro extends Activity {
	private List<Map<String, Object>> breakCallList;
	private Map<String, Object> breakCallData;
	private IntroAdapter introAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.intro);
		this.startActivity(new Intent(this, Loading.class));
		//시작시간 종료시간 현시간으로 셋팅
		View startTimeText = findViewById(R.id.startTime_text);
		View endTimeText = findViewById(R.id.endTime_text);
		if(startTimeText instanceof EditText){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			((TextView) startTimeText).setText(sdf.format(Calendar.getInstance().getTime()));
			((TextView) endTimeText).setText(sdf.format(Calendar.getInstance().getTime()));
		}
		Set<String> defaultSet = new HashSet<String>();
		SharedPreferences pref = getSharedPreferences(Const.PREF_NAME, MODE_PRIVATE);
		String breakCallStr = pref.getString(Const.PREF_KEY, "");
		if(!"".equals(breakCallStr)){
			breakCallStr = "[" + breakCallStr + "]";
			breakCallList = new ArrayList<Map<String,Object>>();

			try {
				JSONArray jsonArray = new JSONArray(breakCallStr);
				for(int i = 0 ; i < jsonArray.length() ; i++){
					JSONObject jsonObject = new JSONObject(jsonArray.get(i).toString());
					
					breakCallData = new HashMap<String, Object>();
					breakCallData.put("breakCallNumber", jsonObject.getString("breakCallNumber"));
					breakCallData.put("breakCallStart", jsonObject.getString("breakCallStart"));
					breakCallData.put("breakCallEnd", jsonObject.getString("breakCallEnd"));

					breakCallList.add(breakCallData);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			introAdapter = new IntroAdapter(this, R.layout.intro_breakcall_row, breakCallList);
			ListView listView = (ListView) findViewById(R.id.breakList_ListView);
			listView.setAdapter(introAdapter);
		}
		else{
			//Break Call 리스트 셋팅(TEST)
			breakCallList = new ArrayList<Map<String,Object>>();
			for(int i = 0 ; i < 20 ; i++){
				breakCallData = new HashMap<String, Object>();
				breakCallData.put("breakCallRow", "테스트"+i);
				breakCallList.add(breakCallData);
			}
			introAdapter = new IntroAdapter(this, R.layout.intro_breakcall_row, breakCallList);
			ListView listView = (ListView) findViewById(R.id.breakList_ListView);
			listView.setAdapter(introAdapter);
			
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void setEndTimeTextAdd(View view){
		View endTimeText = findViewById(R.id.endTime_text);
		String endTimeTextStr = ((TextView) endTimeText).getText().toString();
		
		int hourBtn = Integer.parseInt(((Button) view).getText().toString());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(sdf.parse(endTimeTextStr));
			calendar.add(Calendar.HOUR, hourBtn);
			
			((TextView) endTimeText).setText(sdf.format(calendar.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setBreakCallExec(View view){
		//입력 전화번호
		View breakCallNumber = findViewById(R.id.breakCallNumber);
		String breakCallNumberStr = ((TextView) breakCallNumber).getText().toString();
		//시작시간
		View startTimeText = findViewById(R.id.startTime_text);
		String startTimeTextStr = ((TextView) startTimeText).getText().toString();
		//종료시간
		View endTimeText = findViewById(R.id.endTime_text);
		String endTimeTextStr = ((TextView) endTimeText).getText().toString();
		//SharedPreference 선언부
		SharedPreferences pref = getSharedPreferences(Const.PREF_NAME, MODE_PRIVATE);
		String str = pref.getString("breakCall", "");
		if(!"".equals(str)){
			str = str + ",";
		}
		//입력데이터 JSON 데이터로 만듬
		str = str + "{\"breakCallNumber\":\"" + breakCallNumberStr + "\",\"breakCallStart\":\"" + startTimeTextStr + "\",\"breakCallEnd\":\"" + endTimeTextStr + "\"}";
		//SharedPreference 데이터 저장
		SharedPreferences.Editor editor = pref.edit();
		editor.putString(Const.PREF_KEY, str);
		editor.commit();
	}
}
